//Page Object
Page({
  data: {
    list:[
      {
        text:"1111111",
        checked:false,
        // flag:false
      },
      {
        text:"2222222",
        checked: true,
        // flag: false
      },
      {
        text:"333333",
        checked:false,
        // flag: false
      },
    ],
    val:"123",
    flag:false,
    activeCount:1
  },
  getval(e){
    this.setData({
      val:e.detail.value
    })
  },
  changeChecked(e){
    var index = e.target.dataset.index;
    var list=this.data.list;
    list[index].checked=!list[index].checked;
    var arr=[];
    list.forEach(element => {
      if(element.checked){
        arr.push(element)
      }
    });
    this.setData({
      list:list,
      activeCount:arr.length
    })
  },
  addItem(){
    var item={
      text:this.data.val,
      checked:false
    }
    this.data.list.push(item);
    this.setData({
      list: this.data.list
    })
  },
  delChecked(){
    var list=this.data.list;
    var arr=[];
    list.forEach(ele=>{
      if(!ele.checked){
        arr.push(ele)
      }
    })
    this.setData({
      list:arr,
      activeCount:0
    })
  },
  selectedAll(){
    var list = this.data.list;
    var arr=[];
    list.forEach(ele=>{
      ele.checked=!this.data.flag;
      if(ele.checked){
        arr.push(ele)
      }
    })
    this.setData({
      flag:!this.data.flag,
      list:list,
      activeCount:arr.length
    })
  },
  del(e){
    var index=e.target.dataset.index;
    var list=this.data.list;
    list.splice(index,1);
    var arr = [];
    list.forEach(ele => {
      if (ele.checked) {
        arr.push(ele)
      }
    })
    this.setData({
      list:list,
      activeCount:arr.length
    })
  },
  changeText(e){
    var index = e.target.dataset.index;
    var value=e.detail.value;
    var list = this.data.list;
    list[index].text=value
    this.setData({
      list:list
    })
  }
  
});

